<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('advertising.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('advertising.position.title')" prop="location_id">
            <el-select v-model="dataForm.location_id" :placeholder="i18n('common.please_select') + i18n('advertising.position.title')">
              <el-option v-for="(v, k) in positionlist" :label="v.title" :key="k" :value="v.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('advertising.product_id')" prop="product_id">
            <el-select v-model="dataForm.product_id" :placeholder="i18n('common.please_select') + i18n('advertising.product_id')">
              <el-option v-for="(v, k) in productList" :label="v.title" :key="k" :value="v.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('advertising.picture')" prop="picture">
            <el-upload
              class="avatar-uploader"
              :action="$http.adornUrl('/file/picture')"
              :show-file-list="false"
              :headers="upload_headers"
              :on-success="handlePictureSuccess"
              :before-upload="beforePictureUpload">
              <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
            <el-link class="ml10" @click="handlePictureRemove" type="danger">
              {{ i18n('common.delete') }}
            </el-link>
          </el-form-item>

          <el-form-item :label="i18n('common.sort')" prop="sort">
            <el-input-number :placeholder="i18n('common.please_input') + i18n('common.sort')" v-model="dataForm.sort"></el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('module:advertising:handle')" type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="advertising-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('advertising')

  const positionlist = ref([])
  const productList = ref([])

  const dataForm = reactive({
    id: 0,
    location_id: '',
    product_id: '',
    picture: '',
    sort: 0,
  })

  const dataRule = reactive({
    location_id: [{
      required: true,
      message: i18n('advertising.rules.location_id.require'),
      trigger: 'blur',
    }],
    product_id: [{
      required: true,
      message: i18n('advertising.rules.product_id.require'),
      trigger: 'blur',
    }],
    picture: [{
      required: true,
      message: i18n('advertising.rules.picture.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/advertising/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.location_id = data.data.location_id
            dataForm.product_id = data.data.product_id
            dataForm.picture = data.data.picture
            dataForm.sort = data.data.sort
          }
        })
      }
    })
  }

  const loadPositionList = () => {
    $http({
      url: $http.adornUrl('/advertising/position/select'),
      method: 'get',
      params: $http.adornParams({
        is_open: 1,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        positionlist.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadProductList = () => {
    $http({
      url: $http.adornUrl('/business/product/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        productList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  if(query.location_id) {
    dataForm.location_id = parseInt(query.location_id)
  }

  const handlePictureRemove = () => {
    dataForm.picture = ''
  }

  onMounted(() => {
    init(formRef)

    loadPositionList()
    loadProductList()
  })
</script>
